package com.wanxi.step07test.controller;


import com.wanxi.step07test.dto.ResultDTO;
import com.wanxi.step07test.model.UserModel;
import com.wanxi.step07test.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Controller
@CrossOrigin
public class UserController {

    public static HashMap<String,String> loginName = new HashMap<>();

    @Autowired
    private UserServiceImpl userService;


//    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO login(UserModel user, HttpServletRequest req) {
//        String vCode = CodeController.code;
//        user.setVCode(vCode);

        ResultDTO resultDTO = userService.login(user);

        if("success".equals(resultDTO.getMsg())){
            loginName.put(user.getUsername(),user.getUsername());
        }

        return resultDTO;
    }


    @PreAuthorize("hasAuthority('user:findAll')")
    @RequestMapping("/back/user/findAll")
    @ResponseBody
    public ResultDTO findAll(UserModel user) {
        ResultDTO resultDTO = userService.findAll(user);
        return resultDTO;
    }

    @PreAuthorize("hasAuthority('user:add')")
    @RequestMapping("/back/user/add")
    @ResponseBody
    public ResultDTO add(UserModel user) {
        ResultDTO resultDTO = userService.add(user);
        return resultDTO;
    }

    @PreAuthorize("hasAuthority('user:delete')")
    @RequestMapping("/back/user/delete")
    @ResponseBody
    public ResultDTO delete(UserModel user) {
        ResultDTO resultDTO = userService.delete(user);
        return resultDTO;
    }

    @PreAuthorize("hasAuthority('user:findById')")
    @RequestMapping("/back/user/findById")
    @ResponseBody
    public ResultDTO findById(UserModel user) {
        ResultDTO resultDTO = userService.findById(user);
        return resultDTO;
    }


    @RequestMapping("/back/user/password")
    @ResponseBody
    public ResultDTO password(UserModel user) {
        ResultDTO resultDTO = userService.password(user);
        return resultDTO;
    }

    @PreAuthorize("hasAuthority('user:update')")
    @RequestMapping("/back/user/update")
    @ResponseBody
    public ResultDTO update(UserModel user) {
        ResultDTO resultDTO = userService.update(user);
        return resultDTO;
    }
}
